package aliyun.cx.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import aliyun.cx.admin.entity.Comment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CommentMapper extends BaseMapper<Comment> {
    
    /**
     * 分页查询评论列表（包含关联信息）
     */
    @Select("<script>" +
            "SELECT c.*, " +
            "       u.username as userName, " +
            "       u.avatar_url as userAvatar, " +
            "       a.title as articleTitle, " +
            "       cat.name as categoryName, " +
            "       pc.content as parentContent, " +
            "       pu.username as parentUserName " +
            "FROM comment c " +
            "LEFT JOIN users u ON c.user_id = u.id " +
            "LEFT JOIN article a ON c.article_id = a.id " +
            "LEFT JOIN category cat ON a.category_id = cat.id " +
            "LEFT JOIN comment pc ON c.parent_id = pc.id " +
            "LEFT JOIN users pu ON pc.user_id = pu.id " +
            "<where>" +
            "<if test='userId != null'>AND c.user_id = #{userId}</if>" +
            "<if test='articleId != null'>AND c.article_id = #{articleId}</if>" +
            "<if test='parentId != null'>AND c.parent_id = #{parentId}</if>" +
            "<if test='status != null'>AND c.status = #{status}</if>" +
            "<if test='content != null and content != &quot;&quot;'>AND c.content LIKE CONCAT('%', #{content}, '%')</if>" +
            "</where>" +
            "ORDER BY c.create_time DESC" +
            "</script>")
    IPage<Comment> selectCommentPageWithRelations(
            Page<Comment> page,
            @Param("userId") Long userId,
            @Param("articleId") Long articleId,
            @Param("parentId") Long parentId,
            @Param("status") Integer status,
            @Param("content") String content
    );
    
    /**
     * 根据评论ID获取评论详情（包含关联信息）
     */
    @Select("SELECT c.*, " +
            "       u.username as userName, " +
            "       u.avatar_url as userAvatar, " +
            "       a.title as articleTitle, " +
            "       cat.name as categoryName, " +
            "       pc.content as parentContent, " +
            "       pu.username as parentUserName " +
            "FROM comment c " +
            "LEFT JOIN users u ON c.user_id = u.id " +
            "LEFT JOIN article a ON c.article_id = a.id " +
            "LEFT JOIN category cat ON a.category_id = cat.id " +
            "LEFT JOIN comment pc ON c.parent_id = pc.id " +
            "LEFT JOIN users pu ON pc.user_id = pu.id " +
            "WHERE c.id = #{id}")
    Comment selectCommentWithRelations(@Param("id") Long id);
    
    /**
     * 根据文章ID获取评论列表（包含关联信息）
     */
    @Select("SELECT c.*, " +
            "       u.username as userName, " +
            "       u.avatar_url as userAvatar, " +
            "       a.title as articleTitle, " +
            "       cat.name as categoryName, " +
            "       pc.content as parentContent, " +
            "       pu.username as parentUserName " +
            "FROM comment c " +
            "LEFT JOIN users u ON c.user_id = u.id " +
            "LEFT JOIN article a ON c.article_id = a.id " +
            "LEFT JOIN category cat ON a.category_id = cat.id " +
            "LEFT JOIN comment pc ON c.parent_id = pc.id " +
            "LEFT JOIN users pu ON pc.user_id = pu.id " +
            "WHERE c.article_id = #{articleId} " +
            "ORDER BY c.create_time DESC")
    List<Comment> selectCommentsByArticleWithRelations(@Param("articleId") Long articleId);
    
    /**
     * 根据用户ID获取评论列表（包含关联信息）
     */
    @Select("SELECT c.*, " +
            "       u.username as userName, " +
            "       u.avatar_url as userAvatar, " +
            "       a.title as articleTitle, " +
            "       cat.name as categoryName, " +
            "       pc.content as parentContent, " +
            "       pu.username as parentUserName " +
            "FROM comment c " +
            "LEFT JOIN users u ON c.user_id = u.id " +
            "LEFT JOIN article a ON c.article_id = a.id " +
            "LEFT JOIN category cat ON a.category_id = cat.id " +
            "LEFT JOIN comment pc ON c.parent_id = pc.id " +
            "LEFT JOIN users pu ON pc.user_id = pu.id " +
            "WHERE c.user_id = #{userId} " +
            "ORDER BY c.create_time DESC")
    List<Comment> selectCommentsByUserWithRelations(@Param("userId") Long userId);
}
